package com.phiture.erp.basic.web.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import com.phiture.erp.basic.core.pojo.dto.ErpUserCustomizeConfigPageReqDTO;
import com.phiture.erp.basic.core.pojo.dto.ErpUserCustomizeConfigSaveReqDTO;
import com.phiture.erp.basic.core.pojo.vo.ErpUserCustomizeConfigRespVO;
import com.phiture.erp.basic.core.service.ErpUserCustomizeConfigService;
import com.phiture.erp.basic.dal.entity.ErpUserCustomizeConfigDO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 用户自定义配置")
@RestController
@RequiredArgsConstructor
@RequestMapping("/erp/user-customize-config")
public class ErpUserCustomizeConfigController {

    private final ErpUserCustomizeConfigService userCustomizeConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建用户自定义配置")
    @PreAuthorize("@ss.hasPermission('erp:user-customize-config:create')")
    public CommonResult<Long> createUserCustomizeConfig(@Valid @RequestBody ErpUserCustomizeConfigSaveReqDTO createReqDTO) {
        return success(userCustomizeConfigService.createUserCustomizeConfig(createReqDTO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户自定义配置")
    @PreAuthorize("@ss.hasPermission('erp:user-customize-config:update')")
    public CommonResult<Boolean> updateUserCustomizeConfig(@Valid @RequestBody ErpUserCustomizeConfigSaveReqDTO updateReqDTO) {
        userCustomizeConfigService.updateUserCustomizeConfig(updateReqDTO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新用户自定义配置状态")
    @PreAuthorize("@ss.hasPermission('erp:user-customize-config:update')")
    public CommonResult<Boolean> updateStatus(@RequestParam("id") Long id) {
        userCustomizeConfigService.updateStatus(id);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户自定义配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:user-customize-config:delete')")
    public CommonResult<Boolean> deleteUserCustomizeConfig(@RequestParam("id") Long id) {
        userCustomizeConfigService.deleteUserCustomizeConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户自定义配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:user-customize-config:query')")
    public CommonResult<ErpUserCustomizeConfigRespVO> getUserCustomizeConfig(@RequestParam("id") Long id) {
        ErpUserCustomizeConfigDO userCustomizeConfig = userCustomizeConfigService.getUserCustomizeConfig(id);
        return success(BeanUtils.toBean(userCustomizeConfig, ErpUserCustomizeConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户自定义配置分页")
    @PreAuthorize("@ss.hasPermission('erp:user-customize-config:query')")
    public CommonResult<PageResult<ErpUserCustomizeConfigRespVO>> getUserCustomizeConfigPage(@Valid ErpUserCustomizeConfigPageReqDTO pageReqDTO) {
        PageResult<ErpUserCustomizeConfigDO> pageResult = userCustomizeConfigService.getUserCustomizeConfigPage(pageReqDTO);
        return success(BeanUtils.toBean(pageResult, ErpUserCustomizeConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户自定义配置 Excel")
    @PreAuthorize("@ss.hasPermission('erp:user-customize-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserCustomizeConfigExcel(@Valid ErpUserCustomizeConfigPageReqDTO pageReqDTO,
              HttpServletResponse response) throws IOException {
        pageReqDTO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpUserCustomizeConfigDO> list = userCustomizeConfigService.getUserCustomizeConfigPage(pageReqDTO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户自定义配置.xls", "数据", ErpUserCustomizeConfigRespVO.class,
                        BeanUtils.toBean(list, ErpUserCustomizeConfigRespVO.class));
    }

}